using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ESoft.DataAccess;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace ESoft.DataAccess.EfCore.MySql
{
    /// <summary>
    /// 
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="service"></param>
        /// <param name="connectStr"></param>
        /// <param name="assemblies"></param>
        public static void AddEfCoreMySql(this IServiceCollection service, string connectStr, params Assembly[] assemblies)
        {
            DatabaseInitializer.AddMapperAssembly(assemblies);
            service.AddDbContext<DataContext>(options => options.UseMySql(connectStr, b =>
            {
                foreach (Assembly assembly in assemblies)
                {
                    b.MigrationsAssembly(assembly.FullName);
                }
            }));
            service.AddTransient<IBaseReponsitory, BaseRepositry>();

            service.AddTransient<ISqlRepositry, MySqlQueryRepositry>();
        }
    }
}